package cn.iocoder.yudao.boot.web.controller;


import cn.iocoder.yudao.boot.common.pojo.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

public abstract class MybatisController<S extends IService<T>, T> {

    @Autowired
    protected S service;

    @PostMapping("/add")
    public R<Boolean> add(@RequestBody T t){
        return R.ok(service.save(t));
    }

    @PutMapping("/update")
    public R<Boolean> update(@RequestBody T t){
        return R.ok(service.updateById(t));
    }

    @DeleteMapping("/delete/{id}")
    public R<Boolean> delete(@PathVariable("id") Serializable id){
        boolean b = service.removeById(id);
        return R.ok(b);
    }

    @GetMapping("/get/{id}")
    public R<T> get(@PathVariable("id") Serializable id){
        T t = service.getById(id);
        return R.ok(t);
    }

    @GetMapping("/list")
    public R<List<T>> list(){
        List<T> list = service.list();
        return R.ok(list);
    }

    @GetMapping("/page")
    public R<IPage<T>> page(@RequestParam("page") Integer page, @RequestParam("size") Integer size, T t){
        Page<T> result = service.page(new Page<T>(page, size), new LambdaQueryWrapper<>(t));
        return R.ok(result);
    }
}
